package com.cmdi.opensite.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cmdi.opensite.entity.Comment;
import com.cmdi.opensite.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 评价控制器
 */
@RestController
@RequestMapping("/api/comments")
public class CommentController {
    
    @Autowired
    private CommentService commentService;
    
    /**
     * 发表评价
     */
    @PostMapping
    public ResponseEntity<?> addComment(@RequestBody Comment comment, @RequestAttribute("userId") Long userId) {
        comment.setUserId(userId);
        boolean success = commentService.addComment(comment);
        if (success) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "评价成功");
            return ResponseEntity.ok(result);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "评价失败，您已评价过此活动");
        return ResponseEntity.badRequest().body(result);
    }
    
    /**
     * 删除评价
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteComment(@PathVariable Long id, @RequestAttribute("userId") Long userId) {
        boolean success = commentService.deleteComment(id, userId);
        if (success) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "删除成功");
            return ResponseEntity.ok(result);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "删除失败");
        return ResponseEntity.badRequest().body(result);
    }
    
    /**
     * 查询活动的评价列表
     */
    @GetMapping("/activity/{activityId}")
    public ResponseEntity<?> getActivityComments(@PathVariable Long activityId,
                                             @RequestParam(defaultValue = "1") Integer current,
                                             @RequestParam(defaultValue = "10") Integer size) {
        IPage<Comment> page = commentService.getActivityComments(
            new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size), activityId);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", page.getRecords());
        result.put("total", page.getTotal());
        result.put("current", current);
        result.put("size", size);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 检查是否已评价
     */
    @GetMapping("/check/{activityId}")
    public ResponseEntity<?> checkComment(@PathVariable Long activityId, @RequestAttribute("userId") Long userId) {
        boolean commented = commentService.hasCommented(activityId, userId);
        Map<String, Object> result = new HashMap<>();
            result.put("commented", commented);
            return ResponseEntity.ok(result);
    }
    
    /**
     * 获取活动的平均评分
     */
    @GetMapping("/activity/{activityId}/average-score")
    public ResponseEntity<?> getAverageScore(@PathVariable Long activityId) {
        double score = commentService.getAverageScore(activityId);
        Map<String, Object> result = new HashMap<>();
        result.put("averageScore", score);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 获取活动的评价数量
     */
    @GetMapping("/activity/{activityId}/count")
    public ResponseEntity<?> getCommentCount(@PathVariable Long activityId) {
        int count = commentService.getCommentCount(activityId);
        Map<String, Object> result = new HashMap<>();
        result.put("commentCount", count);
        return ResponseEntity.ok(result);
    }
}